package com.teadent.servlet;

import com.teadent.bean.Person;
import com.teadent.bean.StudentTask;
import com.teadent.bean.Task;
import com.teadent.service.TeacherService;
import com.teadent.service.TeacherServiceImpl;
import org.apache.catalina.tribes.transport.RxTaskPool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/tea.action")
public class TeacherServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        String action=req.getParameter("action");
        TeacherService ts=new TeacherServiceImpl();
        Person person=(Person) req.getSession().getAttribute("session_person");
        
        //对学生信息的删查
        if (action.equals("list")||action.equals("delThisStu") ||action.equals("addStudent") )

        {
            if(action.equals("delThisStu")){
                String userId=req.getParameter("userId");
                ts.delThisStu(userId);
            }
            else if(action.equals("addStudent")){
                req.setAttribute("mainRight","addStudent.jsp");
                req.getRequestDispatcher("main.jsp").forward(req,resp);
                String studentId=req.getParameter("studentId");
                String studentName=req.getParameter("studentName");
                String studentSex=req.getParameter("studentSex");
                String studentEmail=req.getParameter("studentEmail");
                String userOthername="普通学生";
                if(studentId.equals("")||studentName.equals("")){
                    req.setAttribute("tip","添加失败,学生名或学号不能为空");
                }
                //String userName, String userId, String userAccount,
                //                  String userPassword, String userEmail, String userSex,
                //                  String userOthername, int userIdentify
                else{
                    Person person0=new Person(studentName,studentId,studentId,
                            studentId,studentEmail,studentSex,userOthername,0);
                    ts.addStudent(person0);
                    req.setAttribute("tip","添加成功");
                }
            }
            List<Person>arr=ts.getAllStudent();
            req.setAttribute("arr",arr);
            req.setAttribute("mainRight","person.jsp");
            req.getRequestDispatcher("main.jsp").forward(req,resp);

        }
        //对学生信息的添加

        //展示全部任务列表与删除任务
        else if(action.equals("goMyTask")||action.equals("delThisTask")){
            if(action.equals("delThisTask")){
                String taskAccount=req.getParameter("taskAccount");
                ts.delThisTask(taskAccount);
            }
            List<Task> arr=ts.getAllTask();
            req.setAttribute("arr",arr);
            req.setAttribute("mainRight","Task.jsp");
            req.getRequestDispatcher("main.jsp").forward(req,resp);
        }
        //修改任务信息
        else if (action.equals("fixThisTask")){
            String taskAccount=req.getParameter("taskAccount");
            Task task=ts.getTask(taskAccount);
            req.setAttribute("task",task);
            req.setAttribute("mainRight","fixTask.jsp");
            req.getRequestDispatcher("main.jsp").forward(req,resp);
        }

        else if(action.equals("fixTask")){

            String taskName=req.getParameter("taskName");
            String taskRequirement=req.getParameter("taskRequirement");
            String taskDate=req.getParameter("taskDate");
            String taskAccount=req.getParameter("taskAccount");
            System.out.println(taskAccount);
            taskDate.replace("T", " ");
            Task task=new Task(taskName,taskRequirement,taskDate);
            ts.fixTask(task,taskAccount);

            List<Task> arr=ts.getAllTask();
            req.setAttribute("arr",arr);
            req.setAttribute("mainRight","Task.jsp");
            req.getRequestDispatcher("main.jsp").forward(req,resp);
        }
        else if(action.equals("goShowDetail")){
            //"tea.action?action=goShowDetail&taskAccount="+taskAccount
            String taskAccount=req.getParameter("taskAccount");
            List<StudentTask> arr=ts.getTaskDetail(taskAccount);
        }
        /*tea.action?action=addTask*/
        else if (action.equals("goAddTask")||action.equals("addTask")){
            req.setAttribute("mainRight","addTask.jsp");
            req.getRequestDispatcher("main.jsp").forward(req,resp);

            if(action.equals("addTask")){
                String taskName=req.getParameter("taskName");
                String taskRequirement=req.getParameter("taskRequirement");
                String taskDate=req.getParameter("taskDate");
                taskDate.replace("T", " ");
                if(taskName.trim().equals("")){
                    req.setAttribute("tip","添加失败,任务名不能为空");
                }else{
                    Task task=new Task(taskName, taskRequirement,taskDate);
                    ts.addTask(task);
                    req.setAttribute("tip","添加成功");
                }
            }

        }


        else if(action.equals("delThisTask")){
            /*"tea.action?action=delThisTask&taskAccount="+taskAccount;*/
            String taskAccount=req.getParameter("taskAccount");
            ts.delThisTask(taskAccount);

        }

        /*else if(action.equals("deleteThisStu")){

        }
        else if(action.equals("fixThisStu")){

        }*/
    }


}
